<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 文章控制器
 */
class Article extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->assign("action", "article");
        $this->load->model('bbs_detail_model');
    }
    //文章首页
    function index() {
        $kw_arr = explode(",", $this->website_config['article_hot_key']);
        $search_word = $kw_arr[rand(0, count($kw_arr)-1)];
        //文章首页文章列表
        $articles_list = $this->article_list();
        $banners_list = $this->banners_round();
        $this->assign("search_word", $search_word);
        $this->assign("articles", $articles_list);
        $this->assign("banners", $banners_list);
        $this->display('article/index.html');
    }
    
    //文章详情页
    function detial($article_id){
        $article_id = decrypt_article_id($article_id);
        $query_params = "a.id,a.title,a.authorid,a.content,a.thumb,a.summary,a.commentnum,a.tags_name,i.head_img,i.nickname,a.create_time";
        $item = $this->db->query("select $query_params from tan_bbs_article a left join tan_users_info i on i.user_id = a.authorid where a.id = $article_id and a.status = 1 order by a.id desc limit 1")->row_array();
        if(!$item){
            $this->display('deleted/index.html');exit;
        }
        $item['space_url'] = site_url("u/".  encrypt_user_id($item['authorid']));
        $item['create_time'] = $this->set_time_three($item['create_time']);
        $item['head_img'] = strstr($item['head_img'],"http") ? $item['head_img'] : $this->img_host.$item['head_img'];
        
        //判断是否关注
        $is_fan = $this->userinfo ? ($this->bbs_user_model->get_fan_status(array("user_id"=>$item['authorid'],"followuid"=>$this->user_id,"is_del"=>1)) ? 1 : 0) : 0;
        //判断是否收藏
        $is_favorite = $this->userinfo ? ($this->bbs_detail_model->get_article_favorite_status(array("user_id"=>$this->user_id,"article_id"=>$article_id,"is_del"=>1)) ? 1 : 0) : 0;
        //判断是否点赞
        $is_like = $this->userinfo ? ($this->bbs_detail_model->get_article_like_status(array("user_id"=>$this->user_id,"article_id"=>$article_id)) ? 1 : 0) : 0;
        
        $tags = array();
        if($item['tags_name']){
            $tags_arr = explode(",", $item['tags_name']);
            foreach ($tags_arr as $v){
                $v = explode("_", $v);
                $tags[$v[1]] = array("tid"=>$v[1],"tname"=>$v[0]); 
            }
        }
        $item['tags'] = $tags;
        
        $repeat_str = array("<br>",'<p><span style="font-size: 13.3333px;"></span></p>',"<p><br></p>","<p>&nbsp;</p>",'<p><span style="color: rgb(51, 51, 51);">&nbsp;</span></p>','<p><span style="color: rgb(62, 62, 62);">&nbsp;</span></p>','<p><strong><span style="font-size: 10.5pt; color: black;">&nbsp;</span></strong></p>','<p><span style="font-size: 10.5pt; color: black;">&nbsp;</span></p>');
        $item['content'] = str_replace($repeat_str, "", $item['content']);
        $item['article_id'] = encrypt_article_id($item['id']);
        $item['author_id'] = encrypt_user_id($item['authorid']);
        $item['detail_url'] = site_url("p/".$item['article_id']);
        
        
        //你可能还喜欢的文章
        $like = "(";
        $count = count($tags);
        $index = 0;
        foreach ($tags as $key => $tag){
            $index += 1;
            if($count == $index){
                $like .= " (a.tags_name like '%$tag[tname]%')";
            }else{
                $like .= " (a.tags_name like '%$tag[tname]%') or ";
            }

        }
        $like .= ")";
        $like_article = $this->db->query("select a.id,a.thumb,a.title,a.authorid,a.commentnum,a.readnum,u.head_img,u.nickname from tan_bbs_article a left join tan_users_info u on u.user_id = a.authorid where a.id not in($article_id) and $like order by a.sort desc limit 8")->result_array();
        foreach ($like_article as $key =>$val){
            $like_article[$key]['article_id'] = encrypt_article_id($val['id']);
            $like_article[$key]['authorid'] = encrypt_user_id($val['authorid']);
            $like_article[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $like_article[$key]['detail_url'] = site_url("p/".  encrypt_article_id($val['id']));
        }
        //打赏记录
        $reward = $this->db->select("r.user_id,r.target_id,r.create_time,u.head_img,u.nickname")
                ->from("bbs_reward r")
                ->join("users_info u","u.user_id = r.user_id","left")
                ->where(array("r.target_id"=>$article_id,"r.target_type"=>'article'))->order_by("r.id desc")->group_by("r.user_id")->get()->result_array();
        foreach ($reward as $key=>$val){
            
            $reward[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $reward[$key]['space_url'] = site_url("u/".encrypt_user_id($val['user_id']));
        }
        $this->assign('reward', $reward);
        
        $item['is_fan'] = $is_fan;
        $item['is_favorite'] = $is_favorite;
        $item['is_like'] = $is_like;
        $this->assign("site_title", $item['title']);
        $this->assign("like_article",$like_article);
        $this->assign("item", $item);
        $this->display('article/detial.html');
    }
    
    //未审核页面
    function upub($article_id){
        $article_id = decrypt_article_id($article_id);
        $query_params = "a.id,a.title,a.authorid,a.content,a.thumb,a.summary,a.commentnum,i.head_img,i.nickname,a.create_time";
        $item = $this->db->query("select $query_params from tan_bbs_article a left join tan_users_info i on i.user_id = a.authorid where a.id = $article_id  order by a.id desc limit 1")->row_array();
        $item['head_img'] = strstr($item['head_img'],"http") ? $item['head_img'] : $this->img_host.$item['head_img'];
        $this->assign("title", $item['title']);
        $this->assign('item',$item);
        $this->display("article/checking.html");
    }
    //文章首页轮播banner
    private function banners_round(){
        $list = $this->db->query("select * from tan_banner where type = 5 and status = 1 order by sort desc")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['banner_link'] = str_replace("www", "wx", $val['banner_link']);
        }
        return $list;
    }
    //文章首页文章列表
    private function article_list(){
//        $list = $this->memcached->get("www_hot_article");
//        if($list){
//            $list = unserialize($list);
//        }else{
            $query_params = "a.id,a.authorid,a.title,a.thumb,a.content,a.readnum,a.commentnum,a.create_time,u.head_img,u.nickname";
            $list = $this->db->query("select $query_params from tan_bbs_article a left join tan_users_info u on u.user_id = a.authorid where a.status = 1 and a.hot = 1 and a.show = 1 order by a.hot desc,a.sort desc limit 27")->result_array();
//        }
        
        foreach ($list as $key=>$val){
            $matches = array();
            preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/i", $val['content'], $matches);
            $content_thumb = $matches[1][0] ? $matches[1][0] : "";
            $list[$key]['content_thumb'] = $content_thumb;
            $list[$key]['detail_url'] = site_url("p/".  encrypt_article_id($val['id']));
            $list[$key]['space_url'] = site_url("u/". encrypt_user_id($val['authorid']));
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $list[$key]['article_id'] = encrypt_article_id($val['id']);
        }
        return $list;
    }
    
    //文章搜索页面
    function search(){
        $kw = trim($_REQUEST['kw']);
        $kw_arr = explode(",", $this->website_config['article_hot_key']);
        $this->assign("kw_arr", $kw_arr);
        $this->assign("keyword", $kw);
        $this->display("article/search.html");
    }
    //文章搜索结果页面
    function search_result(){
        $kw = trim($_REQUEST['kw']);
        $query_params = "a.id,a.authorid,a.title,a.thumb,a.content,a.readnum,a.commentnum,a.create_time,u.head_img,u.nickname";
        $list = $this->db->query("select $query_params from tan_bbs_article a left join tan_users_info u on u.user_id = a.authorid where a.title like '%$kw%' and a.status = 1 and a.show = 1 order by a.hot desc,a.sort desc limit 15")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['detail_url'] = site_url("p/".  encrypt_article_id($val['id']));
            $list[$key]['space_url'] = site_url("u/". encrypt_user_id($val['authorid']));
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $list[$key]['title'] = str_replace($kw, "<span class='col-red'>$kw</span>", $val['title']);
            $list[$key]['article_id'] = encrypt_article_id($val['id']);
        }
        $this->assign("list", $list);
        $this->assign("keyword", $kw);
        $this->display("article/search_result.html");
    }
}
